from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'../../../../../python_230901/homework/liuyingge/zuoye/01_02/discuz_POM_pro')
from web_base.base_page import BasePage


class DiscuzPage(BasePage):
    url = r'http://192.168.68.136/upload/'
    # 登录定位
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    login_btn_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em')

    # 退出定位
    logout_btn_loc = (By.LINK_TEXT, '退出')

    # 注册定位
    register_btn_loc = (By.LINK_TEXT, '立即注册')
    username1_loc = (By.ID, 'y99G29')
    password1_loc = (By.ID, 'IsW5M7')
    Confirm_loc = (By.ID, 'vK663s')
    newemail_loc = (By.ID, 'LOosck')
    submit_btn_loc = (By.ID, 'registerformsubmit')

    # 看贴定位
    plate_btn_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    read_btn_loc = (By.CSS_SELECTOR, '#normalthread_805 > tr > th > a.s.xst')

    # 发帖定位
    post_btn_loc = (By.ID, 'newspecial')
    subject_loc = (By.ID, 'subject')
    message_loc = (By.XPATH, '/html/body')
    postsubmit_btn_loc = (By.ID, 'postsubmit')

    # 回帖定位
    reply_btn_loc = (By.CSS_SELECTOR, '#post_reply > img')
    reply_loc = (By.XPATH, '//*[@id="postmessage"]')
    replysubmit_btn_loc = (By.CSS_SELECTOR, '#postsubmit > span')

    def __init__(self, driver):
        super().__init__(driver)
        self.get_url(self.url)

    def login(self, username, password):
        self.ele_clear(self.username_loc)
        self.input_ele(self.username_loc, username)
        self.ele_clear(self.password_loc)
        self.input_ele(self.password_loc, password)
        self.ele_click(self.login_btn_loc)
        sleep(3)

    def logout(self, username='admin', password=123456):
        self.login(username, password)
        self.ele_click(self.logout_btn_loc)
        sleep(3)

    def register(self, username1, password1, Confirm, email):
        self.ele_clear(self.username1_loc)
        self.input_ele(self.username1_loc, username1)
        self.ele_clear(self.password1_loc)
        self.input_ele(self.password1_loc, password1)
        self.ele_clear(self.Confirm_loc)
        self.input_ele(self.Confirm_loc, Confirm)
        self.ele_clear(self.email_loc)
        self.input_ele(self.email_loc, email)
        self.ele_click(self.register_btn_loc)
        sleep(3)

    def read(self, username, password):
        self.login(username, password)
        self.ele_click(self.plate_btn_loc)
        self.ele_click(self.read_btn_loc)
        sleep(3)

    def post(self, subject, message, username='admin', password='123456'):
        self.login(username, password)
        self.ele_click(self.plate_btn_loc)
        sleep(1)
        self.ele_click(self.post_btn_loc)
        sleep(1)
        self.ele_clear(self.subject_loc)
        self.input_ele(self.subject_loc, subject)
        self.driver.switch_to.frame('e_iframe')
        self.ele_clear(self.message_loc)
        self.input_ele(self.message_loc, message)
        sleep(1)
        self.driver.switch_to.default_content()
        self.ele_click(self.postsubmit_btn_loc)
        sleep(3)

    def reply(self, connect, username='admin', password='123456'):
        self.login(username, password)
        self.ele_click(self.plate_btn_loc)
        self.ele_click(self.read_btn_loc)
        sleep(3)
        self.ele_click(self.reply_btn_loc)
        self.ele_clear(self.reply_loc)
        self.input_ele(self.reply_loc, connect)
        sleep(3)
        self.ele_click(self.replysubmit_btn_loc)
        sleep(3)


if __name__ == '__main__':
    username = 'admin'
    password = 123456

    chrome_location = r'E:\python\chrome\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location
    driver = webdriver.Chrome(options=options)

    discuz_page = DiscuzPage(driver)
    discuz_page.login(username, passwprd)

    driver.quit()
